package com.dhy.commadstate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dhy.commadstate.util.CommandSettings;
import com.dhy.commadstate.util.IBaseAdapter;
import com.dhy.commadstate.util.MyCommand;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    CommandSettings settings;
    ListView listView;
    AutoCompleteTextView commandCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String fileName = getIntent().getStringExtra(FILE_NAME);
        settings = new CommandSettings(this, fileName).load();
        list = settings.getCommands();
        TextView title = (TextView) findViewById(R.id.textViewTitle);
        title.setText(fileName);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter(this));
        commandCode = (AutoCompleteTextView) findViewById(R.id.editTextCommandCode);
        commandCode.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getNames()));
        commandCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                showCommand(commandCode.getText().toString());
            }
        });
    }

    private static final String FILE_NAME = "file_name";

    public static void start(Context context, String fileName) {
        Intent i = new Intent(context, MainActivity.class);
        i.putExtra(FILE_NAME, fileName);
        context.startActivity(i);
    }

    List<MyCommand> list;

    List<String> getNames() {
        List<String> list = new ArrayList<>();
        for (MyCommand c : this.list) {
            list.add(c.name);
        }
        return list;
    }

    void showCommand(String input) {
        if (input.isEmpty()) return;
        List<MyCommand> list = this.list;
        int index = -1;
        try {
            Integer.parseInt(input);
            for (int i = 0, listSize = list.size(); i < listSize; i++) {
                MyCommand c = list.get(i);
                String fullCode = String.valueOf(c.code);
                if (fullCode.equals(input) || fullCode.endsWith(input)) {
                    index = i;
                    break;
                }
            }
        } catch (Exception e) {
            for (int i = 0, listSize = list.size(); i < listSize; i++) {
                if (list.get(i).name.equals(input)) {
                    commandCode.setText("");
                    index = i;
                    break;
                }
            }
        }
        if (index != -1) listView.setSelection(index);
    }

    public void reset(View view) {
        for (MyCommand command : list) {
            command.log = false;
            command.testing = false;
            settings.update(command);
        }
        settings.save();
        listView.setAdapter(new MyAdapter(this));
    }

    class MyAdapter extends IBaseAdapter<MyCommand> implements CompoundButton.OnCheckedChangeListener {

        public MyAdapter(Context context) {
            super(context, MainActivity.this.list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getView(position, convertView, parent, R.layout.item_layout);
            TextView code = (TextView) findViewById(R.id.textViewCode);
            TextView name = (TextView) findViewById(R.id.textViewName);
            CheckBox log = (CheckBox) findViewById(R.id.checkBoxLog);
            CheckBox testing = (CheckBox) findViewById(R.id.checkBoxTesting);
            MyCommand c = getItem(position);
            code.setText(String.valueOf(c.code));
            name.setText(c.name);
            log.setOnCheckedChangeListener(null);
            testing.setOnCheckedChangeListener(null);
            log.setChecked(c.log);
            testing.setChecked(c.testing);
            log.setTag(position);
            testing.setTag(position);
            log.setOnCheckedChangeListener(this);
            testing.setOnCheckedChangeListener(this);
            return convertView;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int position = (int) buttonView.getTag();
            MyCommand c = getItem(position);
            if (buttonView.getId() == R.id.checkBoxLog) {
                if (c.log != isChecked) {
                    c.log = isChecked;
                    Toast.makeText(context, "save ok:" + settings.save(c), Toast.LENGTH_SHORT).show();
                }
            } else {
                if (c.testing != isChecked) {
                    c.testing = isChecked;
                    Toast.makeText(context, "save ok:" + settings.save(c), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
